package com.hnevc.androidhomework6;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class MainActivity2 extends AppCompatActivity {
    //数据
    ArrayList<Meal> mealArrayList;
    //ListView
    ListView mLvMeal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.antivity_meal_demo);

        //初始化界面所需的数据
        initData();
        //初始化界面
        initView();
        //设置事件
        initEvent();
    }

    private void initEvent() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String username = bundle.getString("username");
        mLvMeal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity2.this,""+position,Toast.LENGTH_SHORT).show();
                //跳转到新的Activity，显示position位置的详细详细
                Intent intent = new Intent(MainActivity2.this,DetailActivity.class);
                //携带数据
                intent.putExtra("id",mealArrayList.get(position).getId());
                intent.putExtra("name",mealArrayList.get(position).getName());
                intent.putExtra("pic",mealArrayList.get(position).getPic());
                intent.putExtra("desc",mealArrayList.get(position).getDesc());
                intent.putExtra("hot",mealArrayList.get(position).getHot());
                intent.putExtra("price",mealArrayList.get(position).getPrice());
                intent.putExtra("sour",mealArrayList.get(position).getSour());
                intent.putExtra("vegetable",mealArrayList.get(position).getVegetable());
                intent.putExtra("meat",mealArrayList.get(position).getVegetable());
                //跳转
                startActivity(intent);
            }
        });
    }

    private void initView() {
        mLvMeal =(ListView) findViewById(R.id.lv_meal);
        MyBaseAdapter adapter = new MyBaseAdapter();
        mLvMeal.setAdapter(adapter);

    }

    private void initData() {
        // read String from news.json
        String str = readNewsJsonFile();
        // parse str to ArrayList<News>
        mealArrayList = parseJSON(str);
    }

    private ArrayList<Meal> parseJSON(String str) {

        //创建链表
        ArrayList<Meal> mealList = new ArrayList<>();

        try {
            JSONArray jsonArray = new JSONArray(str);
            for (int i=0;i < jsonArray.length();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Meal meal = new Meal();
                meal.setId(jsonObject.optString("food_id"));
                meal.setName(jsonObject.optString("food_name"));
                meal.setPic(jsonObject.optString("food_pic"));
                meal.setDesc(jsonObject.optString("food_desc"));
                meal.setHot(jsonObject.optString("food_hot"));
                meal.setPrice(jsonObject.optString("food_price"));
                meal.setVegetable(jsonObject.optString("food_vegetable"));
                meal.setSour(jsonObject.optString("food_sour"));
                meal.setMeat(jsonObject.optString("food_meat"));



                //将news添加到newsList中
                mealList.add(meal);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return  mealList;
    }

    private String readNewsJsonFile() {

        String str="";
        InputStream inputStream = getResources().openRawResource(R.raw.foodlist);
        try {
            //inputStream.available()返回有效的字节数
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);//读取到字节数组
            inputStream.close();//关闭流
            str = new String(bytes);
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return  str;
        }

    }

    class MyBaseAdapter extends BaseAdapter{
        //显示项目数目
        @Override
        public int getCount() {
            return mealArrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return mealArrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //从项目中创建view
            View view = View.inflate(
                    MainActivity2.this,
                    R.layout.list_item_layout,
                    null);
            //从项目布局视图中获取图片控件
            ImageView icon = view.findViewById(R.id.iv_id);
            //修改图片控件显示的图片
            //获取当前数据的图片名称
            String pic = mealArrayList.get(position).getId();
            //根据文件名，获取资源id
            int mipmap = getResources().getIdentifier(
                    pic.substring(0,pic.indexOf(".")),
                    "mipmap",
                    getPackageName());
            icon.setImageResource(mipmap);
            //从项目布局视图中获取文本控件
            TextView tvName = view.findViewById(R.id.tv_name);
            //获取当前新闻的标题，并在tvName显示
            tvName.setText(mealArrayList.get(position).getName());
            //返回
            TextView tvPrice = view.findViewById(R.id.tv_price);
            tvPrice.setText(mealArrayList.get(position).getPrice()+"元");
            return view;
        }
    }
    class  Meal{
        String id;
        String name;
        String price;
        String desc;
        String pic;
        String hot;
        String sour;
        String vegetable;
        String meat;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public String getHot() {
            return hot;
        }

        public void setHot(String hot) {
            this.hot = hot;
        }

        public String getSour() {
            return sour;
        }

        public void setSour(String sour) {
            this.sour = sour;
        }

        public String getVegetable() {
            return vegetable;
        }

        public void setVegetable(String vegetable) {
            this.vegetable = vegetable;
        }
        public String getMeat() {
            return meat;
        }

        public void setMeat(String meat) {
            this.meat = meat;
        }

    }
}